package com.htthorizon.powersupply.controller;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.htthorizon.powersupply.domain.AdsCstCjbdConsVoltloweDi;
import com.htthorizon.powersupply.service.AdsCstCjbdConsVoltloweDiService;
import com.htthorizon.powersupply.utils.OrgUtil;
import com.htthorizon.powersupply.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Brilliant
 */
@RestController
@RequestMapping("/adsCstCjbdConsVoltloweDi")
public class AdsCstCjbdConsVoltloweDiController {

    @Autowired
    private AdsCstCjbdConsVoltloweDiService adsCstCjbdConsVoltloweDiService;

//    @RequestMapping("/getByTgNo")
//    public R<List<AdsCstCjbdConsVoltloweDi>> getByTgNo(@RequestMapping(value = "tgNo") String tgNo)

    @GetMapping("/getByTgNo")
    public R<List<AdsCstCjbdConsVoltloweDi>> getByTgNo(@RequestParam(value = "tgNo",required = false) String tgNo,
                                                       @RequestParam(value="tgStdCountyOrgNo", required = false)String tgCountyOrgNo,
                                                       @RequestParam(value="tgStdCountyOrgNm", required = false)String tgCountyOrgNm,
                                                       @RequestParam(value="tgStdOrgNo", required = false)String tgOrgNo,
                                                       @RequestParam(value="tgStdOrgNm", required = false)String tgOrgNm,
                                                       @RequestParam(value="lineNm", required = false)String lineNm,
                                                       @RequestParam(value = "tgNm",required = false)String tgNm,
                                                       @RequestParam(value = "consNm",required = false)String consNm
    ) {
        LambdaQueryWrapper<AdsCstCjbdConsVoltloweDi> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(tgNo),AdsCstCjbdConsVoltloweDi::getTgNo, tgNo);
        wrapper.eq(StringUtils.isNotEmpty(tgCountyOrgNo),AdsCstCjbdConsVoltloweDi::getStdCountyOrgNo, tgCountyOrgNo);
        wrapper.eq(StringUtils.isNotEmpty(tgCountyOrgNm),AdsCstCjbdConsVoltloweDi::getStdCountyOrgNm, tgCountyOrgNm);
        wrapper.eq(StringUtils.isNotEmpty(tgOrgNo),AdsCstCjbdConsVoltloweDi::getStdOrgNo, tgOrgNo);
        wrapper.like(StringUtils.isNotEmpty(tgOrgNm),AdsCstCjbdConsVoltloweDi::getStdOrgNm, tgOrgNm);
        wrapper.like(StringUtils.isNotEmpty(lineNm),AdsCstCjbdConsVoltloweDi::getLineNm, lineNm);
        wrapper.like(StringUtils.isNotEmpty(tgNm),AdsCstCjbdConsVoltloweDi::getTgNm, tgNm);
        wrapper.like(StringUtils.isNotEmpty(consNm),AdsCstCjbdConsVoltloweDi::getConsNm, consNm);

        DateTime today = DateUtil.date();
        DateTime offset = DateUtil.offset(today, DateField.DAY_OF_MONTH, -1);
        wrapper.eq(AdsCstCjbdConsVoltloweDi::getDataDt, offset.toString("yyyy-MM-dd 00:00:00"));
        List<AdsCstCjbdConsVoltloweDi> adsCstCjbdConsVoltloweDiList = adsCstCjbdConsVoltloweDiService.list(wrapper);
        if (!adsCstCjbdConsVoltloweDiList.isEmpty()) {
            for (AdsCstCjbdConsVoltloweDi adsCstCjbdConsVoltloweDi : adsCstCjbdConsVoltloweDiList){
                adsCstCjbdConsVoltloweDi.setStdCountyOrgNm(OrgUtil.map(adsCstCjbdConsVoltloweDi.getStdCountyOrgNm()));
            }
        }
        return R.ok(adsCstCjbdConsVoltloweDiList);
    }
}
